package com.xhai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhai.common.constant.ConfigConstants;
import com.xhai.common.constant.DataConstants;
import com.xhai.common.exception.ServiceException;
import com.xhai.common.utils.classes.CollectionUtils;
import com.xhai.common.utils.i18n.MessageUtils;
import com.xhai.domain.entity.SysConfig;
import com.xhai.mapper.SysConfigMapper;
import com.xhai.service.SysConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 参数表 服务实现类
 * </p>
 *
 * @author songhai
 * @since 2025-05-26
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Override
    public List<SysConfig> getConfigByCode(String code) {
        return this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, code)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));
    }

    @Override
    public boolean selectCaptchaCheckEnabled() {
        List<SysConfig> captchaEnabled = this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, ConfigConstants.SYS_CAPTCHA_CHECK)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));

        if (CollectionUtils.isEmpty(captchaEnabled)) {
            throw new ServiceException(MessageUtils.message("config.captcha.check.enable.not.exists"));  //("验证码开启参数缺失，请联系管理员");
        } else if (captchaEnabled.size() > 1) {
            throw new ServiceException(MessageUtils.message("config.captcha.check.enable.exists.many"));  //("验证码开启参数存在多个，请联系管理员");
        } else if (ConfigConstants.CONFIG_ENABLE.equals(captchaEnabled.get(0).getConfigValue())) {
            return true;
        }

        return false;
    }

    @Override
    public String selectCaptchaCheckMode() {
        // 获取验证码校验方式
        List<SysConfig> captchaCheckMode = this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, ConfigConstants.SYS_CAPTCHA_CHECK_MODE)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));
        if (CollectionUtils.isEmpty(captchaCheckMode)) {
            throw new ServiceException(MessageUtils.message("config.captcha.check.mode.not.exists"));  //("验证码校验方式参数缺失，请联系管理员");
        } else if (captchaCheckMode.size() > 1) {
            throw new ServiceException(MessageUtils.message("config.captcha.check.mode.exists.many"));  //("验证码校验方式参数存在多个，请联系管理员"");
        }
        return captchaCheckMode.get(0).getConfigValue();
    }

    @Override
    public String selectLoginBlackIPList() {
        List<SysConfig> loginBlackIPList = this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, ConfigConstants.SYS_LOGIN_BLACK_IP_LIST)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));
        if (CollectionUtils.isEmpty(loginBlackIPList)) {
            throw new ServiceException(MessageUtils.message("config.login.black.ip.list.not.exists"));  //("用户登录黑名单列表参数缺失，请联系管理员");
        } else if (loginBlackIPList.size() > 1) {
            throw new ServiceException(MessageUtils.message("config.login.black.ip.list.exists.many"));  //("用户登录黑名单列表参数存在多个，请联系管理员"");
        }
        return loginBlackIPList.get(0).getConfigValue();
    }

    @Override
    public Integer selectLoginPasswordFailMaxRetryCount() {
        List<SysConfig> loginPasswordFailMaxRetryCount = this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, ConfigConstants.SYS_LOGIN_PASSWORD_FAIL_MAX_RETRY_COUNT)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));
        if (CollectionUtils.isEmpty(loginPasswordFailMaxRetryCount)) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.max.retry.count.not.exists"));  //("用户登录密码错误最大重试次数参数缺失，请联系管理员");
        } else if (loginPasswordFailMaxRetryCount.size() > 1) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.max.retry.count.exists.many"));  //("用户登录密码错误最大重试次数存在多个，请联系管理员"");
        }
        Integer maxRetryCount = 0;
        try {
            maxRetryCount = Integer.parseInt(loginPasswordFailMaxRetryCount.get(0).getConfigValue());
        } catch (NumberFormatException e) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.max.retry.count.not.number"));  //("用户登录密码错误最大重试次数参数值不是数字，请联系管理员"");
        }

        return maxRetryCount;
    }

    @Override
    public Integer selectLoginPasswordFailLockTime() {
        List<SysConfig> loginPasswordFailLockTime = this.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, ConfigConstants.SYS_LOGIN_PASSWORD_FAIL_LOCK_TIME)
                .eq(SysConfig::getStatus, DataConstants.STATUS_NORMAL));
        if (CollectionUtils.isEmpty(loginPasswordFailLockTime)) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.lock.time.not.exists"));  //("用户登录密码错误锁定时间参数缺失，请联系管理员");
        } else if (loginPasswordFailLockTime.size() > 1) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.lock.time.exists.many"));  //("用户登录密码错误锁定时间存在多个，请联系管理员"");
        }
        Integer lockTime = 0;
        try {
            lockTime = Integer.parseInt(loginPasswordFailLockTime.get(0).getConfigValue());
        } catch (NumberFormatException e) {
            throw new ServiceException(MessageUtils.message("config.login.password.fail.lock.time.not.number"));  //("用户登录密码错误锁定时间参数值不是数字，请联系管理员"");
        }

        return lockTime;
    }
}
